package com.wsd.jing.validator;

import com.wsd.jing.annotation.Status;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.validator
 * @className: EnumValidator
 * @description 自定枚举参数校验器
 * @createDate 2022/11/28 11:48
 * @copyright 2022/11/28 11:48 www.tom.com Inc. All rights reserved.
 */
public class StatusValidator implements ConstraintValidator<Status, Object> {
    private String[] stringValues;
    private int[] intValues;
    private boolean[] booleanValues;

    @Override
    public void initialize(Status constraintAnnotation) {
        stringValues = constraintAnnotation.stringValues();
        intValues = constraintAnnotation.intValues();
        booleanValues = constraintAnnotation.booleanValues();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        try {
            if (value == null) {
                return true;
            }
            if (value instanceof String) {
                for (String s : stringValues) {
                    if (s.equals(value)) {
                        return true;
                    }
                }
            } else if (value instanceof Integer) {
                for (int s : intValues) {
                    if (s == (Integer) value) {
                        return true;
                    }
                }
            } else if (value instanceof Boolean) {
                for (boolean s : booleanValues) {
                    if (s == (Boolean) value) {
                        return true;
                    }
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }
}
